<?php
namespace Components\ProductsClass\First;

class Validation extends \Phalcon\Validation {
	private $fields;

	public function execute() {
		$result = [];
		$validationMessages = $this -> validate($this -> fields);
		if($validationMessages -> valid()) {
			$result['code'] = -1;
			$result['message'] = $validationMessages[0]->getMessage();
		} else {
			$result['code'] = 0;
			$result['fields'] = $this -> fields;
		}
		return $result;
	}

	public function setFields($fields)  {
		$this -> fields = $fields;
		return $this;
	}

	public function addName() {
		$this -> fields['name'] = $this -> getDI() -> get('filter') -> sanitize($this -> fields['name'], ['string','special_chars','trim']);
		return $this -> rules('name',[
			new \Phalcon\Validation\Validator\PresenceOf()
		]);
	}

	public function addStatus() {
		$this -> fields['status'] = $this -> getDI() -> get('filter') -> sanitize($this -> fields['status'], 'int');
		return $this -> rules('status',[
			new \Phalcon\Validation\Validator\PresenceOf(),
			new \Phalcon\Validation\Validator\InclusionIn([
				'domain' => [0, 1]
			])
		]);
	}
}
